<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    //

	protected $table='task';
	protected $fillable=[
		'store_station_id','type','num','store_id','status','to_department_id','to_store_station_id',
		'start_day','end_day','start_time','end_time','comment'
	];
	protected $hidden=[];

	public function lists($store_id,$uid){
		return $this->with('store_station')
			->with(['task_user'=>function($query) use($uid){
			$query->where(['uid'=>$uid]);
		}])
			->with('department')->with('to_store_station')
			->where(['store_id'=>$store_id,'status'=>1,'type'=>1])
			->where(function ($query){
				$query->where('start_day','<=',date("Y-m-d",time()))->where('end_day','>=',date("Y-m-d",time()));
			})
			->get();
	}

	public function task_list($store_id){
		return $this->with('store_station')->with('department')->with('to_store_station')
			->where(['store_id'=>$store_id,'status'=>1,'type'=>1,'status'=>0])->where('num','>',0)
			->get();
	}
	public function store_station(){
		return $this->belongsTo('App\Model\Store_station','store_station_id')->with('station');
	}

	public function store_stations(){
		return $this->belongsTo('App\Model\Store_station','store_station_id');
	}

	public function to_store_station(){
		return $this->belongsTo('App\Model\Store_station','to_store_station_id');
	}

	public function department(){
		return $this->belongsTo('App\Model\Department','to_department_id');
	}

	public function task_desc($id){
		return $this->where(['id'=>$id])->with('store_station')->first();
	}

	public function update_desc($data){
		return $this->where(['id'=>$data['id']])->update($data);
	}

	public function receipt_list($store_id){
		return $this->where(['type'=>1,'store_id'=>$store_id,'status'=>1])
			->with('store_station')->where('num','>',0)->get();
	}
	public function desc($id){
		return $this->with('store_station')->where(['id'=>$id])->first();
	}

	public function station(){
		return $this->belongsTo('App\Model\Station','station_id');
	}

	//	任务列表
	public function store_task_list($where){
		 $data=$this->where(function ($query)use($where){
			$query->where(['store_id'=>$where['store_id'],'type'=>1]);
			$query->whereBetween('created_at',[$where['start_time'],$where['end_time']]);
		})->with('store_station')->get();
		foreach($data as $kk=>$vv){
			if($vv['store_station']['station']['status']==0){
				$vv['statu']='短期有效';
			}
			if($vv['store_station']['station']['status']==1){
				$vv['statu']='长期有效';
			}
			if($vv['start_day']>date('Y-m-d',time())){
				$vv['staff_statu']='未开始';
				$vv['staff_statu_type']='1';
			}
			if($vv['start_day']<date('Y-m-d',time()) && $vv['end_day']>date('Y-m-d',time())){
				$vv['staff_statu']='进行中';
				$vv['staff_statu_type']='2';
			}
			if($vv['end_day']<date('Y-m-d',time())){
				$vv['staff_statu']='已结束';
				$vv['staff_statu_type']='3';
			}
		}
		$newArr=[];
		foreach($data as $k=>$v){
			if($v['staff_statu_type']==2){
				$newArr['ing'][]=$v;
			}
			if($v['staff_statu_type']==3){
				$newArr['over'][]=$v;
			}
		}
		return $newArr;
	}


	public function task_user(){
		return $this->hasMany('App\Model\task_user','task_id');
	}
}
